package com.sgb.kjwl.view.ui.adapter.self;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.self.VideoEntity;
import com.sgb.knowledgeshare.weight.VideoPalyerView;
import java.util.HashMap;
import java.util.List;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

/**
 * @author cfk
 * @describe
 * @date
 */
public class VideoPagerAdapter extends PagerAdapter {
    private Context context;
    private List<VideoEntity> videoUrl;
    private JCVideoPlayerStandard[] list;
    private VideoPalyerView[] palyerViews;


    public VideoPagerAdapter(Context context, List<VideoEntity> videoUrl) {
        this.context = context;
        this.videoUrl = videoUrl;
        list = new JCVideoPlayerStandard[videoUrl.size()];
        palyerViews = new VideoPalyerView[videoUrl.size()];
    }

    @Override
    public int getCount() {
        return videoUrl == null ? 0 : videoUrl.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.video_item, null);
        VideoPalyerView aliVideo = view.findViewById(R.id.ali_video);
//        JCVideoPlayerStandard videoView = view.findViewById(R.id.video_view);
//        videoView.setUp(videoUrl.get(position).getUrl(), JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL, videoUrl.get(position).getTitle());
//        videoView.thumbImageView.setImageBitmap(setVideoFirstImg(videoUrl.get(position).getUrl()));
//        list[position] = videoView;
        palyerViews[position] = aliVideo;
        palyerViews[position].videoPlayUrl(videoUrl.get(position).getUrl(), false);
        container.addView(view);
        aliVideo.setCallback(() -> {

        });
        aliVideo.start();
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }


    public void startVideo(int position) {
        //释放资源
//        JCVideoPlayerStandard.releaseAllVideos();
//        list[position].startVideo();
        palyerViews[position].start();
    }

    private Bitmap setVideoFirstImg(String url) {
        try {
            MediaMetadataRetriever media = new MediaMetadataRetriever();
            if (url.contains("http")) {
                media.setDataSource(url, new HashMap<>());
            } else {
                media.setDataSource(url);
            }
            Bitmap bitmap = media.getFrameAtTime();
            return bitmap;
        } catch (Exception e) {
            Toast.makeText(context, "封面图加载异常", Toast.LENGTH_SHORT).show();
        }
        return null;
    }
}
